<?php
if ( IPB_THIS_SCRIPT != 'admin' )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'init.php'.";
	exit();
}

class run_upgrade
{
	var $ipsclass;
	var $version_id;
	var $version_name;
	
	var $parse_LIMIT = 500; // IPB regular limit
	
	function auto_run()
	{
		$redirect = array(
							'0' => 'create_tables',
							'1' => 'alter_tables',
							'2' => 'update_category_description',
							'3' => 'update_item_description',
							'4' => 'update_receipt_description',
							'5' => 'update_bank_description',
							'6' => 'update_transfer_settings',
						);
		
		if(!isset($redirect[$this->ipsclass->input['step']]))
		{
			$this->ipsclass->input['step'] = 0;
		}
		
		$func = $redirect[$this->ipsclass->input['step']];
		$this->$func();
		
		// Pre Edit item description
		// Pre Display receipt item decription
		// Update transfer settings 
	}
	
	function create_tables()
	{
		$this->ipsclass->DB->load_cache_file(ROOT_PATH.'sources/market/install/'.$this->ipsclass->vars['sql_driver'].'_database_queries.php','sql_database_queries');
		
		$this->ipsclass->DB->build_query_from_cache('table_market_wages',array(),'sql_database_queries');
		$this->ipsclass->DB->exec_query();
		$this->ipsclass->DB->build_query_from_cache('table_market_wages_collected',array(),'sql_database_queries');
		$this->ipsclass->DB->exec_query();
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=1","Installed new tables...");
	}
	
	function alter_tables()
	{
		$this->ipsclass->DB->sql_change_field('market_items','base_price','base_price',"bigint(19) NOT NULL","'0'");
		$this->ipsclass->DB->sql_change_field('market_items','base_resell','base_resell',"bigint(19) NOT NULL","'0'");
		$this->ipsclass->DB->sql_change_field('market_inventory','price_bought','price_bought',"bigint(19) NOT NULL","'0'");
		$this->ipsclass->DB->sql_change_field('market_inventory','resell_price','resell_price',"bigint(19) NOT NULL","'0'");
		$this->ipsclass->DB->sql_add_field('market_inventory','settings',"text NOT NULL");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=2","Altered existing tables...");
	}
	
	function update_category_description()
	{
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_categories','limit'=>array($this->ipsclass->input['st'],$this->parse_LIMIT)));
		$q = $this->ipsclass->DB->exec_query();
		if($this->ipsclass->DB->get_num_rows() == 0)
		{
			$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=3","Updated category descriptions...");
		}
		while($category = $this->ipsclass->DB->fetch_row($q))
		{
			$this->ipsclass->DB->do_update('market_categories',array(
																		'cat_description' => $this->ipsclass->parser->pre_display_parse($category['cat_description'])
									),"cat_id=".$category['cat_id']);
		}
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=2&st=".($this->ipsclass->input['st']+$this->parse_LIMIT),"Updating category descriptions...");
	}
	
	function update_item_description()
	{
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_items','limit'=>array($this->ipsclass->input['st'],$this->parse_LIMIT)));
		$q = $this->ipsclass->DB->exec_query();
		if($this->ipsclass->DB->get_num_rows() == 0)
		{
			$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=4","Updated item descriptions...");
		}
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			$this->ipsclass->DB->do_update('market_items',array(
																	'item_description' => $this->ipsclass->parser->pre_edit_parse($item['item_description'])
									),"item_id=".$item['item_id']);
		}
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=3&st=".($this->ipsclass->input['st']+$this->parse_LIMIT),"Updating item descriptions...");
	}
	
	function update_receipt_description()
	{
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_buy_logs','limit'=>array($this->ipsclass->input['st'],$this->parse_LIMIT)));
		$q = $this->ipsclass->DB->exec_query();
		if($this->ipsclass->DB->get_num_rows() == 0)
		{
			$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=5","Updated receipt descriptions...");
		}
		while($log = $this->ipsclass->DB->fetch_row($q))
		{
			$this->ipsclass->DB->do_update('market_buy_logs',array(
																	'item_description' => $this->ipsclass->parser->pre_display_parse($log['item_description'])
										),"log_id=".$log['log_id']);
		}
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=4&st=".($this->ipsclass->input['st']+$this->parse_LIMIT),"Updating receipt descriptions...");
	}
	
	function update_bank_description()
	{
		if(is_array($this->ipsclass->cache['market_plugin']) && count($this->ipsclass->cache['market_plugin']))
		{
			foreach($this->ipsclass->cache['market_plugin'] as $plugin)
			{
				if($plugin['filename'] == "bank.php")
				{
					$bank_info = $plugin;
				}
			}
		}
		if(!is_array($bank_info))
		{
			$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=6","Continuing upgrade...");
		}
		
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_bank_starter','limit'=>array($this->ipsclass->input['st'],$this->parse_LIMIT)));
		$q = $this->ipsclass->DB->exec_query();
		if($this->ipsclass->DB->get_num_rows() == 0)
		{
			$bank = $this->ipsclass->market_class->load_plugin($bank_info);
			$bank->bank_cache();
			
			$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=6","Updated bank starter descriptions...");
		}
		while($starter = $this->ipsclass->DB->fetch_row($q))
		{
			$this->ipsclass->DB->do_update('market_bank_starter',array(
																		's_description' => $this->ipsclass->parser->pre_display_parse($starter['s_description'])
								),"starter_id=".$starter['starter_id']);
		}
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=5&st=".($this->ipsclass->input['st']+$this->parse_LIMIT),"Updating bank starter descriptions...");
	}
	
	function update_transfer_settings()
	{
		if(is_array($this->ipsclass->cache['market_plugin']) && count($this->ipsclass->cache['market_plugin']))
		{
			foreach($this->ipsclass->cache['market_plugin'] as $plugin)
			{
				if($plugin['filename'] == "transfer.php")
				{
					$settings = unserialize($plugin['plugin_config']);
					$settings['allow_items'] = array_keys($this->ipsclass->cache['market_items']);
					$this->ipsclass->DB->do_update('market_plugins',array(
																			'plugin_config' => serialize($settings),
										),"link='".$plugin['link']."'");
				}
			}
		}
	}
	
	// End Upgrade
}
?>